/*
 * @(#) KyodaiApp.java
 *
 * Copyright 2006 - 2009 by the original authors of Galaxy
 * and all its contributors ("Galaxy.org").
 *
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kyodai;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * Main application MIDlet.
 *
 * @author Cheng Liang
 * @version 1.0
 */
public class KyodaiApp extends MIDlet {
	
	/** Game screen in this application. */
	public GameScreen gameScreen;
	
	/** Game model, most important is the map and related operations. */
	public GameModel model;
	
	private Display display;

	public KyodaiApp() {
		display = Display.getDisplay(this);
	}
	
	public void setCurrentScreen(Displayable screen) {
		display.setCurrent(screen);
	}
	
	/**
	 * Initializes game model in the same thread.
	 */
	public void gameModelInit() {
		if(model == null) {
			model = new GameModel();
		}
		model.start(false);
	}

	protected void destroyApp(boolean unconditional) {
		if(gameScreen != null) {
			gameScreen.stop();
			gameScreen = null;
		}
		notifyDestroyed();
	}

	protected void pauseApp() {
		Displayable current = display.getCurrent();
        if(current != null && current instanceof GameScreen) {
        	((GameScreen)current).stop(); // FIXME pause correct
        }
	}

	/* (non-Javadoc)
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
	protected void startApp() throws MIDletStateChangeException {
		gameScreen = new GameScreen(this);
		setCurrentScreen(gameScreen);
		gameScreen.start();
	}

}
